package Vistas;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import BaseDeDatos.DBProductos;
import Clases.MateriaPrima;

public class PedidoStockVista extends JFrame implements ActionListener {
	private static final long serialVersionUID = 1L;
	private MateriaPrima mp;
	private JTextField txtCantidad;
	private JButton btnEnviar;
	
	public PedidoStockVista(MateriaPrima mp) {
		this.mp = mp;
		this.txtCantidad = new JTextField(5);
		this.btnEnviar = new JButton("Pedir stock");
		
		this.btnEnviar.addActionListener(this);
		
		this.add(new JLabel("Pedido de stock para " + mp.getNombre()));
		this.add(this.txtCantidad);
		this.add(this.btnEnviar);
		
		this.setTitle("Stock Insuficiente!!!");
		this.setLayout(new FlowLayout());
		this.setSize(250, 130);
		this.setVisible(true);
	}

	public void actionPerformed(ActionEvent arg0) {
		Integer cantidad = Integer.parseInt(this.txtCantidad.getText());
		
		this.mp.setCantidad(cantidad);
		
		try {
			new DBProductos().updateStock(this.mp);
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		JOptionPane.showMessageDialog(null, "Stock Actualizado");
		
		this.dispose();
	}

}
